package wpspider.client.view;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

import wpspider.client.view.event.ExitButtonEH;
import wpspider.client.view.event.SearchBoxEH;
import wpspider.client.view.event.SearchButtonEH;
import wpspider.client.view.graph.PageGraph;


public class View {
	/** Component repository. */
	private ComponentRepository _repos;
	
	/**
	 * Constructor.
	 */
	public View() {
		_repos = ComponentRepository.getInstance();
	}
	
	/**
	 * Sets visible flag.
	 * @param visibleFlag Visible flag.
	 */
	public void setVisible(boolean visibleFlag) {
		JFrame mainFrame = (JFrame) _repos.getComponent("mainFrame");
		mainFrame.setVisible(true);
	}
	
	/**
	 * Builds view.
	 */
	public void build() throws Exception {
		// Main frame.
		JFrame mainFrame = new JFrame();
		mainFrame.setTitle("WPSpider");
		mainFrame.setSize(500, 600);
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		_repos.setComponent("mainFrame", mainFrame);
		
		// Menu bar.
		JMenuBar menuBar = new JMenuBar();
		mainFrame.setJMenuBar(menuBar);
				
		// Tool bar on the north of base panel.
		JToolBar toolBar = new JToolBar();
		toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));
		mainFrame.getContentPane().add(toolBar, BorderLayout.NORTH);
		
		// Search box on tool bar.
		JComboBox searchBox = new JComboBox();		
		searchBox.addActionListener(new SearchBoxEH());
		searchBox.setEditable(true);		
		toolBar.add(searchBox);
		_repos.setComponent("searchBox", searchBox);
		
		// Buttons on tool bar.
		JButton searchButton = new JButton("Search");
		searchButton.addActionListener(new SearchButtonEH());
		toolBar.add(searchButton);
		JButton stopButton = new JButton("Stop");
		toolBar.add(stopButton);
		JButton startButton = new JButton("Start");
		toolBar.add(startButton);
		toolBar.add(Box.createHorizontalStrut(5));
		JButton exitButton = new JButton("Exit");
		exitButton.addActionListener(new ExitButtonEH());
		toolBar.add(exitButton);
		
		// Main panel on the center of main frame.
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
		
		// Default graph panel.
		//JPanel graphPanel = new JPanel();
		JPanel graphPanel = new PageGraph().getViewer();
		graphPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, 500));
		mainPanel.add(graphPanel);
		_repos.setComponent("graphPanel", graphPanel);

		// Console text area.
		JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setMaximumSize(new Dimension(Short.MAX_VALUE, 100));
		mainPanel.add(scrollPane);
		JTextArea consoleTextArea = new JTextArea();
		consoleTextArea.setEditable(false);
		scrollPane.getViewport().add(consoleTextArea);
		_repos.setComponent("consoleTextArea", consoleTextArea);
		
		// Status bar label.
		JLabel statusLabel = new JLabel("Status label.");		
		mainFrame.getContentPane().add(statusLabel, BorderLayout.SOUTH);
		_repos.setComponent("statusLabel", statusLabel);
		
		mainFrame.setVisible(true);
	}
}
